﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.View.Controls.Dialogs
{
    /// <summary>
    /// InputView.xaml 的交互逻辑
    /// </summary>
    public partial class InputView : UserControl
    {
        static HandyControl.Controls.PopupWindow window;
        public InputView()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 显示输入框
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="isVisCloseBut">界面右上角是否显示关闭按钮</param>
        /// <returns>点击的按钮文本</returns>
        public static string Show(string content, bool isVisCloseBut = true)
        {
            InputView inputView;
            string clikename = null;
            try
            {
                inputView = new InputView();
                inputView.DataContext = new { Title = "提示", Content = content, IsClose = isVisCloseBut };
                inputView.spacingPanel.Children.Clear();
                Button button = new Button()
                {
                    Margin = new Thickness(5),
                    FontSize = 16,
                    Height = 35,
                    Content = "确认",
                    Background = View.Base.Colors.PrimaryColor,
                    BorderBrush = View.Base.Colors.PrimaryColor,
                    Foreground = Brushes.White
                };
                button.Click += (s, e) =>
                {
                    clikename = inputView.tbxInput.Text.Trim();
                    window.Close();
                };
                inputView.spacingPanel.Children.Add(button);
                inputView.tbxInput.KeyUp += (s, e) =>
                {
                    if (e.Key == Key.Enter)
                    {
                        //回车时直接关闭窗口
                        clikename = inputView.tbxInput.Text.Trim();
                        window.Close();
                    }
                };
                window = new HandyControl.Controls.PopupWindow
                {
                    ShowBorder = false,
                    ShowTitle = false,
                    Background = Brushes.Transparent,
                    PopupElement = inputView,
                    WindowStartupLocation = WindowStartupLocation.CenterScreen,
                    AllowsTransparency = true,
                    WindowStyle = WindowStyle.None,
                    MinWidth = 0,
                    MinHeight = 0
                };
                window.ShowDialog();
                inputView = null;
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.ToString());
            }
            return clikename;
        }
        private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                //触摸屏触发时跳过
                if (e.StylusDevice != null)
                {
                    e.Handled = true;
                }
                else
                {
                    window?.DragMove();
                }
            }
            catch (Exception)
            {

                //throw;
            }

        }
        //触摸屏触发
        private void Grid_PreviewTouchDown(object sender, TouchEventArgs e)
        {
            try
            {
                window?.DragMove();
            }
            catch (Exception)
            {

                // throw;
            }

        }
        private void closeClick(object sender, RoutedEventArgs e)
        {
            window?.Close();
        }
    }

}
